package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.PolluterE;
import com.infore.airsupervise.biz.impl.PolluterBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 污染源详情
 *
 * @作者: dingxinlin
 * @创建时间: 2017/4/13 9:24
 */
public class PolluterDetailActivity extends BaseTitleBarActivity {

    @BindView(R.id.polluter_name)
    TextView polluterName;
    @BindView(R.id.mn_code)
    TextView mnCode;
    @BindView(R.id.grid_name)
    TextView gridName;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.polluter_desc)
    TextView polluterDesc;
    @BindView(R.id.law_person)
    TextView lawPerson;
    @BindView(R.id.phone_number)
    TextView phoneNumber;

    private String mnStr;
    private PolluterE polluter;

    private PolluterBizImpl polluterBiz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_source_detail);
        ButterKnife.bind(this);

        mnStr = getIntent().getStringExtra(Constants.INTENT_STR_KEY);

        polluterBiz = new PolluterBizImpl();
        getPolluterDetaildata();

        initView();
    }

    private void initView() {
        title.setText("污染源基本信息");
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_ELLIPSIS_V);
    }

    @Override
    protected void onRightMenuClick(View view) {
        super.onRightMenuClick(view);

        PopupMenu popupMenu = new PopupMenu(ctx, rightMenu);
        popupMenu.getMenuInflater().inflate(R.menu.source_detail_option_menu, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.menu_license:
                        JumpUtil.jumpFromTo(
                                ctx, LicenseListActivity.class, Constants.INTENT_STR_KEY, polluter.getMn());
                        break;
                    case R.id.menu_record:
                        JumpUtil.jumpFromTo(
                                ctx, SiteLedgerListActivity.class, Constants.INTENT_STR_KEY, polluter.getMn());
                        break;
                    case R.id.menu_monitor:
                        JumpUtil.jumpFromTo(
                                ctx, NewAQIActivity.class, Constants.INTENT_STR_KEY, polluter.getMn());
                        break;
                    case R.id.menu_location:
                        JumpUtil.jumpFromTo(
                                ctx, PolluterMapActivity.class, Constants.INTENT_STR_KEY, polluter.getMn());
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
    }

    /**
     * 设置内容
     */
    private void setContent() {
        if (polluter == null) {
            ToastUtil.showLong(ctx, "Intent参数传递失败");
            return;
        }

        polluterName.setText(polluter.getName());
        mnCode.setText(polluter.getMn());
        gridName.setText("所属网格：" + polluter.getGrid());
        address.setText("企业地址：" + polluter.getAddress());
        polluterDesc.setText("描述信息：" + polluter.getDiscrib());
        lawPerson.setText("法人代表：" + polluter.getPerson());
        phoneNumber.setText("联系电话：" + polluter.getPhone());
    }

    /**
     * 获取污染源详情
     */
    private void getPolluterDetaildata() {
        if (mnCode == null || mnStr.isEmpty()) {
            ToastUtil.showLong(ctx, "Intent参数传递失败");
            return;
        }

        final SweetAlertDialog dialog = new SweetAlertDialog(ctx, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING);
        dialog.setCancelable(false);
        dialog.show();

        polluterBiz.getPolluterDetail(mnStr, new OnServerResponseListener<PolluterE>() {
            @Override
            public void onSucceed(PolluterE result, String desc) {
                dialog.dismiss();
                polluter = result;
                setContent();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                dialog.setTitleText(title)
                        .setContentText(desc)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
            }
        });

    }
}
